<template>
    <div><van-nav-bar
  title="黑马头条-登录"
  fixed
  placeholder
  safe-area-inset-top
/>
<van-form @submit="onSubmit">
  <van-field
    v-model="from.mobile"
    name="mobile"
    label="手机号"
    placeholder="手机号"
    required
    :rules="[{ required: true, message: '请填写手机号', pattern:/^1[3-9]\d{9}$/ }]"
  />
  <van-field
    v-model="from.code"
    type="password"
    name="code"
    label="验证码"
    placeholder="验证码"
    required
    :rules="[{ required: true, message: '请填写验证码' , pattern:/^\d{6}$/}]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info"
    :disabled='isLogin'
    native-type="submit"
    :loading='isLogin'
    loading-text="加载中..."
    >提交</van-button>
  </div>
</van-form></div>
</template>
<script>
import { loginAPI } from '@/api'
import { setToken } from '@/utils/token'
import { Notify } from 'vant'
export default {
  data () {
    return {
      from: {
        mobile: '13245678912',
        code: '246810'
      },
      isLogin: false
    }
  },
  methods: {
    async onSubmit (values) {
      this.isLogin = true
      try {
        const res = await loginAPI({
          mobile: this.from.mobile,
          code: this.from.code
        //   ...this.from
        })
        console.log(res)
        setToken(res.data.data.token)
        Notify({ type: 'success', message: '登陆成功' })
        this.isLogin = false
        this.$router.push({
          path: '/layout'
        })
      } catch (err) {
        console.error(err)
        Notify({ type: 'danger', message: '登陆失败' })
        this.isLogin = false
      }
    }
  }

}
</script>
<style lang='less' scoped>
</style>
